# Estimate the unconditional effects of election outcomes on voter emotions party by party,
# and detect outlying observations in terms of intense post-election reactions. See Appendix F.




#############
# load data #
#############

library(brms)
library(ggplot2)
library(ggpubr)
library(stargazer)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")

colnames(president_df)

length(unique(president_df$country)) # 26
length(unique(president_df$electionname)) # 29
length(unique(president_df$party_country)) # 52

# focus on 15 days before and after the election
day_range <- 15

sub <- president_df[which(president_df$daysinceelection >= -1*day_range
                          & president_df$daysinceelection <= day_range),]

# winners and losers
winners <- sub[which(sub$win == 1),]
losers <- sub[which(sub$win != 1),]

length(unique(winners$electionname[which(!is.na(winners$polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$economic_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$social_polarization))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_present))])) # 27
length(unique(winners$electionname[which(!is.na(winners$populist_dummy))])) # 27

length(unique(losers$electionname[which(!is.na(losers$polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$economic_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$social_polarization))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_present))])) # 27
length(unique(losers$electionname[which(!is.na(losers$populist_dummy))])) # 27




#################
# winner + love #
#################

winner_love_party <- data.frame(electionname=unique(winners$electionname),
                                est=NA,
                                lwr=NA,
                                upr=NA)

for(i in 1:nrow(winner_love_party)){
  temp <- winners[which(winners$electionname == winner_love_party$electionname[i]),]
  wl <- lm(loveprop ~ post + Xl + Xr, data=temp)
  winner_love_party[i,2:4] <- c(coef(wl)[2], confint(wl)[2,])
}




##################
# winner + angry #
##################

winner_angry_party <- data.frame(electionname=unique(winners$electionname),
                                 est=NA,
                                 lwr=NA,
                                 upr=NA)

for(i in 1:nrow(winner_angry_party)){
  temp <- winners[which(winners$electionname == winner_angry_party$electionname[i]),]
  wa <- lm(angryprop ~ post + Xl + Xr, data=temp)
  winner_angry_party[i,2:4] <- c(coef(wa)[2], confint(wa)[2,])
}




################
# loser + love #
################

loser_love_party <- data.frame(electionname=unique(losers$electionname),
                               est=NA,
                               lwr=NA,
                               upr=NA)

for(i in 1:nrow(loser_love_party)){
  temp <- losers[which(losers$electionname == loser_love_party$electionname[i]),]
  ll <- lm(loveprop ~ post + Xl + Xr, data=temp)
  loser_love_party[i,2:4] <- c(coef(ll)[2], confint(ll)[2,])
}




##################
# loser + angry #
##################

loser_angry_party <- data.frame(electionname=unique(losers$electionname),
                                est=NA,
                                lwr=NA,
                                upr=NA)

for(i in 1:nrow(loser_angry_party)){
  temp <- losers[which(losers$electionname == loser_angry_party$electionname[i]),]
  la <- lm(angryprop ~ post + Xl + Xr, data=temp)
  loser_angry_party[i,2:4] <- c(coef(la)[2], confint(la)[2,])
}


# make sure all different
cbind(winner_love_party$est, winner_angry_party$est,
      loser_love_party$est, loser_angry_party$est)
cbind(winner_love_party$lwr, winner_angry_party$lwr,
      loser_love_party$lwr, loser_angry_party$lwr)
cbind(winner_love_party$upr, winner_angry_party$upr,
      loser_love_party$upr, loser_angry_party$upr)




###########
# summary #
###########

# figure F.1
#pdf("party_by_party.pdf", width=12, height=7)
par(mfrow=c(2,2))
plot(density(winner_love_party$est),
     main="Estimates of Post Election on Love Proportion\namong Winners")
rug(winner_love_party$est)
abline(v=winner_love_party$est[which(winner_love_party$electionname == "United States_2016")],
       col="red", lwd=2)
plot(density(winner_angry_party$est),
     main="Estimates of Post Election on Angry Proportion\namong Winners")
rug(winner_angry_party$est)
abline(v=winner_angry_party$est[which(winner_angry_party$electionname == "United States_2016")],
       col="red", lwd=2)
plot(density(loser_love_party$est),
     main="Estimates of Post Election on Love Proportion\namong Losers")
rug(loser_love_party$est)
abline(v=loser_love_party$est[which(loser_love_party$electionname == "United States_2016")],
       col="red", lwd=2)
plot(density(loser_angry_party$est),
     main="Estimates of Post Election on Angry Proportion\namong Losers")
rug(loser_angry_party$est)
abline(v=loser_angry_party$est[which(loser_angry_party$electionname == "United States_2016")],
       col="red", lwd=2)
#dev.off()


# check us 2016
winner_love_party$est[which(winner_love_party$electionname == "United States_2016")]
# 3.584025
quantile(winner_love_party$est, 0.73)
#      73% 
# 3.577053 

winner_angry_party$est[which(winner_angry_party$electionname == "United States_2016")]
# -2.347458
quantile(winner_love_party$est, 0.1)
#       10% 
# -2.348765

loser_love_party$est[which(loser_love_party$electionname == "United States_2016")]
# -3.575155
quantile(loser_love_party$est, 0.12)
#     12% 
# -3.5405

loser_angry_party$est[which(loser_angry_party$electionname == "United States_2016")]
# 2.043775
quantile(loser_angry_party$est, 0.77)
#      77% 
# 2.057951


# check outliers (see also analyses in Appendix P)
cutoff <- 2

winner_love_party$outlier <- NA
winner_love_party$outlier[which(winner_love_party$est < (mean(winner_love_party$est) - cutoff * sd(winner_love_party$est)))] <- 1
winner_love_party$outlier[which(winner_love_party$est > (mean(winner_love_party$est) + cutoff * sd(winner_love_party$est)))] <- 1
winner_love_party$electionname[which(winner_love_party$outlier == 1)]
#[1] "United States_2020"

winner_angry_party$outlier <- NA
winner_angry_party$outlier[which(winner_angry_party$est < (mean(winner_angry_party$est) - cutoff * sd(winner_angry_party$est)))] <- 1
winner_angry_party$outlier[which(winner_angry_party$est > (mean(winner_angry_party$est) + cutoff * sd(winner_angry_party$est)))] <- 1
winner_angry_party$electionname[which(winner_angry_party$outlier == 1)]
#[1] "United States_2016" "Slovenia_2017"

loser_love_party$outlier <- NA
loser_love_party$outlier[which(loser_love_party$est < (mean(loser_love_party$est) - cutoff * sd(loser_love_party$est)))] <- 1
loser_love_party$outlier[which(loser_love_party$est > (mean(loser_love_party$est) + cutoff * sd(loser_love_party$est)))] <- 1
loser_love_party$electionname[which(loser_love_party$outlier == 1)]
#[1] "Paraguay_2018" "Peru_2016"

loser_angry_party$outlier <- NA
loser_angry_party$outlier[which(loser_angry_party$est < (mean(loser_angry_party$est) - cutoff * sd(loser_angry_party$est)))] <- 1
loser_angry_party$outlier[which(loser_angry_party$est > (mean(loser_angry_party$est) + cutoff * sd(loser_angry_party$est)))] <- 1
loser_angry_party$electionname[which(loser_angry_party$outlier == 1)]
#[1] "Taiwan_2020"  "Romania_2019" "Georgia_2018"
